package com.joysuch.wwyt.bp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joysuch.wwyt.bp.bean.BpInvestigatePlanReqBean;
import com.joysuch.wwyt.bp.bean.investigate.*;
import com.joysuch.wwyt.bp.bean.investigate.vo.BpInvestigateTaskHisVO;
import com.joysuch.wwyt.bp.bean.investigate.vo.InvestigatePlanVO;
import com.joysuch.wwyt.bp.entity.investigate.BpInvestigatePlan;
import com.joysuch.wwyt.bp.service.BpInvestigatePlanService;
import com.joysuch.wwyt.bp.service.BpInvestigateTaskHisService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import com.joysuch.wwyt.repeatsubmit.NoRepeatSubmit;
import com.joysuch.wwyt.risk.bean.RiskPointControlBean;
import com.joysuch.wwyt.risk.service.RiskEvaluationBusinessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author ZhangDong
 * @Date 2020/3/25 16:19
 */
@RestController
@RequestMapping("/bp/investigate/plan")
@Api(tags = "隐患管理-隐患排查计划")
public class BpInvestigatePlanController {

    @Autowired
    private BpInvestigatePlanService bpInvestigatePlanService;
    @Autowired
    private RiskEvaluationBusinessService evaluationBusinessService;

    @Autowired
    private BpInvestigateTaskHisService bpInvestigateTaskHisService;

    @PostMapping("save")
    //@RequiresPermissions("bp:investigate:plan:save")
    @ApiOperation("新增/修改排查计划")
    public ResultBean save(@RequestBody @Valid BpInvestigatePlanBean bean) {
        return bpInvestigatePlanService.save(bean);
    }

    @PostMapping("saveBatch")
    //@RequiresPermissions("bp:investigate:plan:saveBatch")
    @ApiOperation("新增/修改排查计划配置-批量")
    public ResultBean saveBatch(@RequestBody ControlCheckConfigBean bean) {
        evaluationBusinessService.saveListControl(bean.getControlLevel());
        bpInvestigatePlanService.saveBatch(bean.getInvestigatePlan(), bean.getBatchFlag());
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("saveBatchControl")
    //@RequiresPermissions("bp:investigate:plan:saveBatch")
    @ApiOperation("新增/修改排查计划配置(管控层级)-批量")
    public ResultBean saveBatchControl(@RequestBody RiskPointControlBean bean) {
        evaluationBusinessService.saveListControl(bean);
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("change-status-batch/{flag}")
    @ApiOperation("批量修改计划状态")
    public ResultBean changeStatusBatch(@PathVariable Integer flag, @RequestBody List<Long> contentIds) {
        Integer changeCount = bpInvestigatePlanService.changeStatusBatch(flag, contentIds);
        return ResultBean.success(changeCount);
    }

    @PostMapping("getByContentIds")
    @ApiOperation(value = "根据排查内容查询计划配置", response = BpInvestigatePlan.class)
    @RequiresPermissions("bp:investigate:plan:get")
    public ResultBean getByContentIds(@RequestBody InvestigatePlanQueryBean bean) {
        InvestigatePlanVO plans = bpInvestigatePlanService.getByContentIds(bean.getHiddenDangerContentIds(), bean.getDetailIds());
        return ResultBean.success(plans);
    }

    @GetMapping("/delete/{id}")
    @RequiresPermissions("bp:investigate:plan:delete")
    @ApiOperation(value = "根据排查计划Id删除")
    @ApiImplicitParam(name = "id", value = "排查计划Id", paramType = "path", required = true)
    public ResultBean delete(@PathVariable Long id) {
        return bpInvestigatePlanService.delete(id);
    }

    @GetMapping("/page")
    @ApiOperation(value = "排查计划分页查询", response = BpInvestigatePlan.class)
    public ResultBean list(BpInvestigatePlanQueryBean bean) {
        return bpInvestigatePlanService.page(bean);
    }

    @GetMapping("/task-page")
    @ApiOperation(value = "排查任务状态-分页查询", response = BpInvestigateTaskDto.class)
    public ResultBean taskPage(BpInvestigateTaskQueryBean bean) {
        //return bpInvestigatePlanService.taskPage(bean);
        Page<BpInvestigateTaskHisVO> bpInvestigateTaskHisVOPage = bpInvestigateTaskHisService.pageByCheckPlanId(bean);
        return ResultBean.pageData(bpInvestigateTaskHisVOPage.getRecords(),bpInvestigateTaskHisVOPage.getTotal());
    }

    @GetMapping("/getById/{id}")
    @ApiOperation(value = "排查计划根据主键查询", response = BpInvestigatePlan.class)
    @ApiImplicitParam(name = "id", value = "排查计划id", paramType = "path", required = true)
    public ResultBean getById(@PathVariable Long id) {
        return bpInvestigatePlanService.getById(id, 0);
    }

    @GetMapping("/open-close")
    @ApiModelProperty("计划的启用停用")
    @RequiresPermissions("bp:investigate:plan:open-close")
    public ResultBean openClose(Long id, String state) {
        return bpInvestigatePlanService.openClose(id, state);
    }


    @PostMapping("batchSwitch")
    @ApiModelProperty("计划的批量启用停用")
    public ResultBean batchSwitch(@RequestBody BpInvestigatePlanReqBean bean){
        bpInvestigatePlanService.batchSwitch(bean);
        return ResultBean.defaultSuccessResult();
    }


    @GetMapping("/produce-task")
    @ApiModelProperty("手动生成任务")
    public ResultBean produceTask() {
        bpInvestigatePlanService.produceTaskForOld();
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("/produce-task-for-new")
    @ApiModelProperty("手动生成任务")
    @NoRepeatSubmit(seconds = 60)
    public ResultBean produceTaskForNew() {
        bpInvestigatePlanService.produceTaskForNew();
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("/getList")
    @ApiOperation(value = "返回当前登录人员计划列表")
    public ResultBean getList() {
        List<BpInvestigatePlan> list = bpInvestigatePlanService.getList();
        return ResultBean.success(list);
    }

    @GetMapping("/taskTimeOutCheckByHand")
    @ApiOperation(value = "手动产生未检")
    public ResultBean taskTimeOutCheckByHand() {
        bpInvestigatePlanService.taskTimeOutCheckByHand();
        return ResultBean.success("");
    }

    @PostMapping("/regenTask")
    @ApiModelProperty("重新生成任务")
    public ResultBean regenTask(@RequestBody List<Long> planIds){
        try {
            bpInvestigatePlanService.regenTask(planIds);
        } catch (ApiBusinessException e) {
            e.printStackTrace();
            return ResultBean.fail(e.getCode(), e.getMessage());
        }
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("/regenAllTask")
    @ApiModelProperty("手动生成当天任务")
    public ResultBean regenAllTask(){
        String msg = bpInvestigatePlanService.regenAllTask();
        return ResultBean.success(msg);
    }

    @PostMapping("/getPlanId")
    @ApiOperation(value = "获取计划ID")
    public ResultBean getPlanId(@RequestBody List<Long> contentIds) {
        List<Long> list = bpInvestigatePlanService.getPlanId(contentIds);
        return ResultBean.success(list);
    }
}
